// xc_lshop/business/order/com/deliver.js
var actbill = require('../../model/business_bill')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    orderinfo: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据, disabled: true
   */
  data: {
    op:"deliver",
    modelistindex: -1,
    deliverpop: false,
    popshow: false,
    company:{
      "SF": '顺丰速运',
      "HTKY":'百世快递',
      "ZTO": '中通快递',
      "STO":'申通快递',
      "YTO": '圆通速递',
      "YD":'韵达速递',
      "YZPY": '邮政快递包裹',
      "HHTT":'天天快递',
      "JD": '京东快递',
      "DBL":'德邦快递',
      "ZJS":'宅急送',
      "other": '其它',
      "shop":'店家配送',
    },
    columns: [
      {
        text: '顺丰速运',
        code: "SF"
      },
      {
        text: '百世快递',
        code: "HTKY"
      },
      {
        text: '中通快递',
        code: "ZTO"
      },
      {
        text: '申通快递',
        code: "STO"
      },
      {
        text: '圆通速递',
        code: "YTO"
      },
      {
        text: '韵达速递',
        code: "YD"
      },
      {
        text: '邮政快递包裹',
        code: "YZPY"
      },
      {
        text: '天天快递',
        code: "HHTT"
      },
      {
        text: '京东快递',
        code: "JD"
      },
      {
        text: '德邦快递',
        code: "DBL"
      },
      {
        text: '宅急送',
        code: "ZJS"
      },
      {
        text: '其它',
        code: "other"
      },
      {
        text: '店家配送',
        code: "shop"
      },
    ],
    formdata: {
      expressname: "",
      expresscode: "",
      expressnum: "",
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectExpress() {
      this.setData({
        popshow: true
      })
    },
    onPopClose() {
      this.setData({
        popshow: false
      })
    },
    onConfirm(event) {
      const {
        picker,
        value,
        index
      } = event.detail;
      var updatedata={};
      updatedata['popshow']=false;
      updatedata['formdata.expresscode']=value['code'];
      if(value['code']!='other'){
        updatedata['formdata.expressname']=value['text'];
      }
      this.setData(updatedata)
      // this.setData({
      //   popshow: false,
      //   'formdata.expressname': value['text'],
      //   'formdata.expresscode': value['code']
      // })
    },
    xcopy(ev) {

      var dataset = ev.currentTarget.dataset
      var message = "复制成功"
      if (dataset.message) {
        message = dataset.message
      }
      wx.setClipboardData({
        data: dataset.val,
        success(res) {
          wx.showToast({
            title: message,
          })

        }
      })

    },
    // 表单提交
    formSubmit: function (e) {
      var valid = this.checkForm();
      var that = this;
      if (valid) {
        var data={};
        data['formdata']= this.data.formdata;
        data['formdata']['ordernum'] = this.data.orderinfo.ordernum;
        if(that.data.op=="editdeliver"){
          data['ismodifystatus']=-1;
          var successmessage="修改成功";
        }else{
          data['ismodifystatus']=1;
          var successmessage="发货成功";

        }
        actbill.orderdeliver(data).then(res => {
  
          if (res["status"] == 1) {

            wx.showToast({
              icon: 'success',
              title: successmessage,
            })
            that.setData({
              deliverpop: false
            })
            // var val = e.detail.value; //通过这个传递数据
            var myEventDetail = {
              index: that.data.modelistindex,
              status: 4,//已发货
              op:that.data.op
            } 
            myEventDetail['formdata']=data['formdata'];
           
        
            this.triggerEvent('eventdeliver', myEventDetail) //myevent自定义名称事件，父组件中使用
          } else {
            wx.showToast({
              icon: 'error',
              title: res["message"],
            })
          }
        })
      }

    },
    //验证表单
    checkForm: function (field) {

      if (field) {
        this.checkRule(field)
      } else {
        var valid = true;
        var vildfield = {};
        if(this.data.formdata['expresscode']=='shop'){
          vildfield['expressname'] = true
          vildfield['expressnum'] = true
          vildfield['expresscode'] = true
          valid = true
        }else{
          ["expressnum", "expressname", "expresscode"].forEach(item => {
        
            
            if (this.data.formdata[item]) {
              vildfield[item] = true
            } else {
             
              vildfield[item] = false
              valid = false
             
            
            }
          })
        }
        
        this.setData({
          vildfield
        })
        return valid;
      }

    },
    //监听input输入
    watchInput: function (e) {
      var dataset = e.target.dataset
      this.data.formdata[dataset.field] = e.detail
      this.checkForm(dataset.field)
    },
    //验证规则
    checkRule(field) {
      var updatemodel = {};
      // if (field == "region") {
      //   updatemodel['vildfield.' + field] = (this.data.formdata[field] && this.data.formdata[field]['2']) ? true : false;
      // } else if (field == "checked") {
      //   updatemodel['vildfield.' + field] = (this.data.formdata[field] && this.data.formdata[field] == 1) ? true : false;
      // } else {
      updatemodel['vildfield.' + field] = this.data.formdata[field] ? true : false;
      // }
      this.setData(updatemodel)
    },
    //返回
    Backfunc(e) {
      this.setData({
        deliverpop: false,
      });
    },
    showdeliver(op,modelistindex, item) {
      if(op=="editdeliver"){
        var formdata = {
          expressname: item.expressname,
          expresscode:item.expresscode,
          expressnum:item.expressnum
        }
      }else{
        var formdata = {
          expressname: "",
          expresscode: "",
          expressnum: "",
        }
      }
      
      this.setData({
        op:op,
        modelistindex: modelistindex,
        deliverpop: true,
        orderinfo: item,
        formdata: formdata
      });
     
    }
  }
})